package net.wanho.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.exception.BlockExceptionUtil;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2025-02-13 11:25
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    // private RestTemplate restTemplate;
    private UserClient userClient;

    @GetMapping("/test")
    public String test(){
        // 直接调用目标服务器
        // String str = restTemplate.getForObject("http://localhost:6001/user/test", String.class);

        String str = userClient.test();

        return "consumer---->" + str;
    }

    @SentinelResource(value = "UserFindResource", blockHandlerClass = BlockExceptionUtil.class, blockHandler = "handlerException")
    @GetMapping("/findByName")
    public AjaxResult findByName(String name){
        return userClient.findByName(name);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Integer id){
        return userClient.removeById(id);
    }

    @PostMapping
    public AjaxResult addUser(@RequestBody User user){
        return userClient.addUser(user);
    }

    @PutMapping
    public AjaxResult modifyUser(@RequestBody User user){
        return userClient.modifyUser(user);
    }

    @GetMapping("/query")
    public AjaxResult query(User user){
        Map<String, Object> params = new HashMap<>();
        params.put("id", user.getId());
        params.put("username", user.getUsername());
        params.put("password", user.getPassword());
        return userClient.query(params);
    }
}
